package com.dinadi.yidi.ui.activity;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.dinadi.yidi.R;
import com.dinadi.yidi.adapter.MyCouponsStateAdapter;
import com.dinadi.yidi.base.BaseActivity;
import com.dinadi.yidi.bean.MessageBean;
import com.dinadi.yidi.bean.MyCouponsStateRequest;
import com.dinadi.yidi.bean.MyCouponsStateRequestEntity;
import com.dinadi.yidi.http.HttpListener;
import com.dinadi.yidi.http.interfaces.CommonInterface;
import com.dinadi.yidi.utils.JsonUtil;
import com.dinadi.yidi.utils.SharePreferencesHelper;
import com.dinadi.yidi.utils.pullloadmorerecyclerview.PullLoadMoreRecyclerView;

import java.util.ArrayList;
import java.util.List;

/**
 * author: JiaYu.He
 * Date: 2017/9/20  优惠券列表
 * Time: 14:24
 */
public class MyCouponsStateActivity extends BaseActivity {
    private View view;
    private TextView tvTitle, text_title_right;
    private RelativeLayout layout_no_comment;
    private ImageView img_no_comment;
    private MyCouponsStateAdapter mRefreshAdapter;
    private List<MyCouponsStateRequestEntity> dateBean = new ArrayList<>();
    private String userId, token, skuId, quantity, cartItemIds;
    /*是否显示没有的图片*/
    private boolean isShow = true;


    @Override
    protected void noWifiRefresh() {

    }

    @Override
    protected View getLayout() {
        view = LayoutInflater.from(MyCouponsStateActivity.this).inflate(R.layout.activity_my_order, null);
        return view;
    }

    @Override
    protected void initView() {
        changePager(2);
        PullLoadMoreRecyclerView mPullLoadMoreRecyclerView = (PullLoadMoreRecyclerView) view.findViewById(R.id.pullLoadMoreRecyclerView);
        tvTitle = (TextView) view.findViewById(R.id.text_title);
        tvTitle.setText(R.string.text_user);
        text_title_right = (TextView) view.findViewById(R.id.text_title_right);
        text_title_right.setText(getResources().getString(R.string.text_my_exchange));
        //显示下拉刷新
        //  mPullLoadMoreRecyclerView.setRefreshing(true);
        mPullLoadMoreRecyclerView.setLinearLayout();

        // 不需要下拉刷新
        mPullLoadMoreRecyclerView.setPullRefreshEnable(false);
        //不需要上拉刷新
        mPullLoadMoreRecyclerView.setPushRefreshEnable(false);
        mRefreshAdapter = new MyCouponsStateAdapter(MyCouponsStateActivity.this);
        mPullLoadMoreRecyclerView.setAdapter(mRefreshAdapter);
        layout_no_comment = (RelativeLayout) view.findViewById(R.id.layout_no_comment);
        img_no_comment = (ImageView) view.findViewById(R.id.img_no_comment);
    }

    @Override
    protected void initListener() {
//        mRefreshAdapter.setmOnItemClickListener(new MyCouponsStateAdapter.OnRecyclerViewItemClickListener() {
//            @Override
//            public void onItemClick(View view, String message) {
//                MyCouponsStateActivity.this.finish();
//            }
//        });
        registerClickListener(text_title_right);
    }

    @Override
    protected void clickEvent(View v) {
        switch (v.getId()) {
            case R.id.text_title_right:
                /*兑换码*/
                Intent intent = new Intent(MyCouponsStateActivity.this, ExChangeActivity.class);
                startActivity(intent);
                break;
            default:
                break;
        }

    }

    @Override
    public void onResume() {
        super.onResume();
        String isRefreshCoupon = SharePreferencesHelper.getInstance(MyCouponsStateActivity.this).getString("isRefreshCoupon");
        if ("is".equals(isRefreshCoupon)) {
            mRefreshAdapter.clearData();
            initPullrefresh();
            SharePreferencesHelper.getInstance(MyCouponsStateActivity.this).putString("isRefreshCoupon", "no");
        }

    }

    @Override
    protected void initData() {
        userId = SharePreferencesHelper.getInstance(MyCouponsStateActivity.this).getString("userId");
        token = SharePreferencesHelper.getInstance(MyCouponsStateActivity.this).getString("token");

        Intent intent = getIntent();
        skuId = intent.getStringExtra("skuId");
        quantity = intent.getStringExtra("quantity");
        cartItemIds = intent.getStringExtra("cartItemIds");
        showLoadingDialog(getResources().getString(R.string.dialog_text));
        initPullrefresh();

    }

    HttpListener httpListener = new HttpListener() {

        @Override
        public void OnSuccess(String response) {
            super.OnSuccess(response);
            String errCode = JsonUtil.getFieldValue(response, "errCode");
            String errMsg = JsonUtil.getFieldValue(response, "errMsg");
            if (errCode.equals("0000")) {
                closeLoadingDialog();
                MyCouponsStateRequest bean = JsonUtil.parseJsonToBean(response, MyCouponsStateRequest.class);
                dateBean = bean.getCouponList();
                if (dateBean.size() == 0) {
                    if (isShow == false) {
                        layout_no_comment.setVisibility(View.GONE);
                    } else {
                        layout_no_comment.setVisibility(View.VISIBLE);
                        img_no_comment.setImageResource(R.drawable.icon_no_coupons);
                    }
                }
                mRefreshAdapter.addAllData(dateBean);
            } else {
                closeLoadingDialog();
                toast(errMsg);
            }


        }

        @Override
        public void OnFail(String errorResponse) {
            super.OnFail(errorResponse);
            closeLoadingDialog();
            toast("加载失败");
        }
    };


    private void initPullrefresh() {
        MessageBean bean = new MessageBean();
        bean.setToken(token);
        bean.setUserId(userId);
        if (cartItemIds != null) {
            bean.setCartItemIds(cartItemIds);
        } else {
            bean.setSkuId(skuId);
            bean.setQuantity(quantity);
        }
        CommonInterface messageInterface = new CommonInterface(MyCouponsStateActivity.this, httpListener);
        messageInterface.getInterface("16022", JSON.toJSONString(bean));

    }

}
